<template>
  <div>{{ address }}</div>
  <!-- <div class="dashboard-editor-container">

    <panel-group @handleSetLineChartData="handleSetLineChartData" />

    <el-row :gutter="32" style="padding:16px 16px 0;margin-bottom:32px;">
        <el-card>
            <div slot="header" class="clearfix">
                <span>生产进度</span>
            </div>
            <el-table
                v-loading="loading"
                :data="workorderList"
                row-key="workorderId"
                default-expand-all
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                >
                <el-table-column label="工单编码" width="180" prop="workorderCode" >
                    <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        @click="handleView(scope.row)"
                        v-hasPermi="['mes:pro:workorder:query']"
                    >{{scope.row.workorderCode}}</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="订单编号" width="140" align="center" prop="sourceCode" />            
                <el-table-column label="客户名称" align="center" prop="clientName" :show-overflow-tooltip="true"/>
                <el-table-column label="产品编号" width="120" align="center" prop="productCode" />
                <el-table-column label="产品名称" width="250px" align="center" prop="productName" :show-overflow-tooltip="true"/>
                <el-table-column label="规格型号" align="center" prop="productSpc" :show-overflow-tooltip="true"/>
                <el-table-column label="单位" align="center" prop="unitOfMeasure" />
                <el-table-column label="生产进度" align="center" width="200px" >
                    <template slot-scope="scope">
                        <el-progress :text-inside="true" :stroke-width="20" :percentage="parseFloat(((scope.row.quantityProduced/scope.row.quantity*100).toFixed(2)))"></el-progress>
                    </template>
                </el-table-column>            
                <el-table-column label="需求日期" align="center" prop="requestDate" width="180">
                    <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.requestDate, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>         
            </el-table>
        </el-card>
    </el-row>
    
    <el-row style="background:#fff;padding:16px 16px 0; margin-bottom:32px;">
      <line-chart :chart-data="lineChartData" />
    </el-row>

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <raddar-chart />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <bar-chart />
        </div>
      </el-col>
    </el-row>

    
  </div>-->
</template>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.3&key=f69e54286070ab6aeab4c85529f9e212"></script>
<script>
import { listWorkorder } from "@/api/mes/pro/workorder";
import PanelGroup from "./dashboard/PanelGroup";
import LineChart from "./dashboard/LineChart";
import RaddarChart from "./dashboard/RaddarChart";
import PieChart from "./dashboard/PieChart";
import BarChart from "./dashboard/BarChart";

const lineChartData = {
  newVisitis: {
    expectedData: [100, 120, 161, 134, 105, 160, 165],
    actualData: [120, 82, 91, 154, 162, 140, 145]
  },
  messages: {
    expectedData: [200, 192, 120, 144, 160, 130, 140],
    actualData: [180, 160, 151, 106, 145, 150, 130]
  },
  purchases: {
    expectedData: [80, 100, 121, 104, 105, 90, 100],
    actualData: [120, 90, 100, 138, 142, 130, 130]
  },
  shoppings: {
    expectedData: [130, 140, 141, 142, 145, 150, 160],
    actualData: [120, 82, 91, 154, 162, 140, 130]
  }
};

export default {
  name: "Index",
  components: {
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    BarChart
  },
  data() {
    return {
      loading: true,
      queryParams: {},
      workorderList: [],
      lineChartData: lineChartData.newVisitis,
      address: null
    };
  },
  created() {
    // this.getList();
    // this.MapLoader();
    // this.addDateRange()
  },
  mounted() {
    // this.MapLoader()
  },
  methods: {
    getList() {
      this.loading = true;
      listWorkorder(this.queryParams).then(response => {
        this.workorderList = response.data;
        this.loading = false;
      });
    },
    handleSetLineChartData(type) {
      this.lineChartData = lineChartData[type];
    },
    MapLoader() {
      let that = this;
      let aMapScript = document.createElement("script");
      // "https://webapi.amap.com/maps?v=1.4.11&key=&plugin=AMap.CitySearch"
      aMapScript.setAttribute(
        "src",
        "https://webapi.amap.com/maps?v=1.4.15&key=ea7a5d156e67765574afec4a64c2e7f6&AMap.Geolocation"
      );
      document.head.appendChild(aMapScript);
      return (aMapScript.onload = function() {
        AMap.plugin("AMap.Geolocation", function() {
          var geolocation = new AMap.Geolocation({
            // 是否使用高精度定位，默认：true
            enableHighAccuracy: true,
            // 设置定位超时时间，默认：无穷大
            timeout: 10000,
            // 定位按钮的停靠位置的偏移量，默认：Pixel(10, 20)
            buttonOffset: new AMap.Pixel(10, 20),
            //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
            zoomToAccuracy: true,
            //  定位按钮的排放位置,  RB表示右下
            buttonPosition: "RB",
            extensions: "all"
          });

          geolocation.getCurrentPosition();
          AMap.event.addListener(geolocation, "complete", onComplete);
          AMap.event.addListener(geolocation, "error", onError);
          function onComplete(data) {
            // data是具体的定位信息
            console.log(data);
            let lng = data.position.lng;
            console.log(data.position.lng, "=======lng====");
            let lat = data.position.lat;
            let lnglatXY = [lng, lat];
            that.init(lnglatXY);
            // geocoder.getAddress(lnglatXY, function(status, result) {
            //   if (status === "complete" && result.info === "OK") {
            //     var address = result.regeocode.formattedAddress;
            //     // that.address = address; //兑换地址
            //     console.log(result,address,"address===========");
            //     // that.$emit("getaddress", that.address, lnglatXY);
            //   } else {
            //     ElMessage.error("无地址，请重新选择");
            //   }
            // });
            console.log("123123");
          }

          function onError(data) {
            console.log(data);
            console.log("123123s");
            // 定位出错
          }
        });
      });
    },

    init(lnglatXY) {
      // let aMapScriptq = document.createElement("script");
      // aMapScriptq.setAttribute(
      //   "src",
      //   "https://restapi.amap.com/maps?v=1.4.15&key=&AMap.Geocoder"
      // );
      // document.head.appendChild(aMapScriptq);
      let xy = lnglatXY.join(",");
      let url = `https://restapi.amap.com/v3/geocode/regeo?key=8a6140378cfc5a0d759bd042a623c89a&location=${xy}&extensions=all`;
      // console.log(xy, aMapScriptq, "======xy=======");
      fetch(url)
        .then(response => response.json())
        .then(data => {
          console.log(data, "=====data=====");
          this.address = data.regeocode.formatted_address;
        });

      // AMapLoader.load({
      //   key: "0f2a1b6ee126f663c150c0debe99b547", // 问题出在这个key，这里类型是个大坑! !
      //   version: "1.4.15", // 默认为 1.4.15,
      //   plugins: ["AMap.Geocoder"]
      // }).then(AMap => {
      //   console.log("AMap", AMap);
      //   const geocoder = new AMap.Geocoder();
      //   geocoder.getAddress(lnglatXY, function(status, result) {
      //     console.log("status, result", status, result);
      //     if (status === "complete" && result.regeocode) {
      //       const address = result.regeocode.formattedAddress;
      //     } else {
      //       console.log("查询失败");
      //     }
      //   });
      // });

      // return (aMapScriptq.onload = function() {
      //   AMap.plugin("AMap.Geocoder", function() {
      //     var geocoder = new AMap.Geocoder({
      //       radius: 1000,
      //       extensions: "all"
      //     });
      //     geocoder.getAddress(xy, function(status, result) {
      //       console.log(lnglatXY, status, result);
      //       if (status === "complete" && result.info === "OK") {
      //         var address = result.regeocode.formattedAddress;
      //         console.log(result, address, "=========adadress=====");
      //         // that.address = address; //兑换地址
      //         // that.$emit("getaddress", that.address, lnglatXY);
      //       } else {
      //         console.log("无地址，请重新选择");
      //         // ElMessage.error("无地址，请重新选择");
      //       }
      //     });
      // });
      // });
    }
  }
};
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width: 1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
